package com.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.reggie.common.BaseContext;
import com.reggie.entity.shoppingCart;
import com.reggie.service.shoppingCartService;
import lombok.extern.slf4j.Slf4j;
import com.reggie.common.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@Transactional
@RequestMapping("/shoppingCart")
public class shoppingCartController {
    //注入shoppingCartService
    @Autowired
    private shoppingCartService shoppingCartService;

    /**
     * @param cart
     * @param session
     * @return com.reggie.common.R<java.lang.String>
     * @description: 添加商品到购物车中
     * @author 86187
     * @date: 2022/11/24 15:43
     */

    @PostMapping("/add")
    public R<String> addIntoShoppingCart(@RequestBody shoppingCart cart, HttpSession session) {
        LambdaQueryWrapper<shoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //给此次的商品添加userId，通过同一线程添加
        if (BaseContext.get() != null) {
            cart.setUserId(BaseContext.get());
        } else {
            cart.setUserId((Long) session.getAttribute("user"));
        }
        //查询购物车里是否存在相同的订单，若口味，菜品，用户相同，则在原基础上添加份数
        queryWrapper.eq(cart.getUserId() != null, shoppingCart::getUserId, cart.getUserId());
        queryWrapper.eq(cart.getDishFlavor() != null, shoppingCart::getDishFlavor, cart.getDishFlavor());
        queryWrapper.eq(cart.getDishId() != null, shoppingCart::getDishId, cart.getDishId());
        shoppingCart temp = shoppingCartService.getOne(queryWrapper);
        if (temp != null) {
            //如果查询到，则让份数加上传进来的份数
            cart.setNumber(cart.getNumber() != null ? cart.getNumber() : 1);
            temp.setNumber(temp.getNumber() + cart.getNumber());
            temp.setCreateTime(LocalDateTime.now());
            //然后进行更新操作
            boolean updateFlag = shoppingCartService.updateById(temp);
            if (updateFlag) {
                return R.success("添加成功!");
            } else {
                return R.error("添加失败!");
            }
        }
        //如果temp为空，则说明数据库中不存在相同的数据
        cart.setCreateTime(LocalDateTime.now());
        boolean save = shoppingCartService.save(cart);
        if (save) {
            return R.success("添加成功!");
        }
        return R.error("添加失败");
    }

    /**
     * @param session
     * @return com.reggie.common.R<java.util.List < com.reggie.entity.shoppingCart>>
     * @description: 查询购物车数据，并返回
     * @author 86187
     * @date: 2022/11/24 15:53
     */

    @GetMapping("/list")
    public R<List<shoppingCart>> selectShoppingForList(HttpSession session) {

        //获取userid
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<shoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(userId != null, shoppingCart::getUserId, userId);
        List<shoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * @param session
     * @return com.reggie.common.R<java.lang.String>
     * @description: 执行清空购物车操作
     * @author 86187
     * @date: 2022/11/24 15:53
     */

    @DeleteMapping("/clean")
    public R<String> cleanAllShopping(HttpSession session) {
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<shoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(userId != null, shoppingCart::getUserId, userId);
        boolean remove = shoppingCartService.remove(queryWrapper);
        if (remove) {
            return R.success("清空购物车成功!");
        }
        return R.error("清空购物车失败!请稍后再试!");
    }

    /**
     * @param cart
     * @param session
     * @return com.reggie.common.R<java.lang.String>
     * @description: 删减购物车选中菜品的份数
     * @author 86187
     * @date: 2022/11/24 16:19
     */


    @PostMapping("/sub")
    public R<String> subShopping(@RequestBody shoppingCart cart, HttpSession session) {
        //获取用户id
        Long userId = (Long) session.getAttribute("user");
        //创建lambda对象
        LambdaQueryWrapper<shoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(userId != null, shoppingCart::getUserId, userId);
        queryWrapper.eq(cart.getDishId() != null, shoppingCart::getDishId, cart.getDishId());
        queryWrapper.eq(cart.getSetmealId() != null, shoppingCart::getSetmealId, cart.getSetmealId());
        //先进行查询，看相对应的菜品数量是否为1
        shoppingCart shoppingCart = shoppingCartService.getOne(queryWrapper);
        if (shoppingCart.getNumber() != 1) {
            shoppingCart.setNumber(shoppingCart.getNumber() - 1);
            boolean update = shoppingCartService.update(shoppingCart, queryWrapper);
            if (update) {
                return R.success("修改成功!");
            } else {
                return R.success("修改失败!请稍后再试!");
            }
        }
        //如果number不满足条件，则进行删除
        boolean remove = shoppingCartService.remove(queryWrapper);
        if (remove) {
            return R.success("菜品已进行移除!");
        }
        return R.success("菜品移除失败!请稍后再试!");
    }
}
